const provinceList = require("./city.js");

const region = {
  getRegionByCode: (code0, code1, code2) => {
    let reg = {
      data: [[], [], []],
      index: [0, 0, 0],
      code: ["", "", ""]
    };
    for (var item0 = 0; item0 < provinceList.length; item0++) {
      // 记录所有省份
      reg.data[0].push({
        code: provinceList[item0].code,
        name: provinceList[item0].name,
      });
      // 如果省份code对应找到
      if (provinceList[item0].code == code0) {
        // 记录省份对应的Index
        reg.index[0] = item0;
        reg.code[0] = provinceList[item0].code;
        // 遍历市列表
        let citys = provinceList[item0].sub;
        for (var item1 = 0; item1 < citys.length; item1++) {
          // 记录当前省下的所有市
          reg.data[1].push({
            code: citys[item1].code,
            name: citys[item1].name,
          });
          // 如果市code对应找到
          if (citys[item1].code == code1) {
            // 记录市对应的Index
            reg.index[1] = item1;
            reg.code[1] = citys[item1].code;
            let areas = citys[item1].sub;
            for (var item2 = 0; item2 < areas.length; item2++) {
              reg.data[2].push({
                code: areas[item2].code,
                name: areas[item2].name
              });
              if (areas[item2].code == code2) {
                reg.code[2] = areas[item2].code;
                reg.index[2] = item2;
              }
            }
          }
        }
      }
    }

    return reg;
  },

  getRegionByIndex: (index0, index1, index2) => {
    let reg = {
      data: [[], [], []],
      index: [0, 0, 0],
      code: ["", "", ""]
    };
    for (var item0 = 0; item0 < provinceList.length; item0++) {
      // 记录所有省份
      reg.data[0].push({
        code: provinceList[item0].code,
        name: provinceList[item0].name,
      });
      // 如果省份code对应找到
      if (item0 == index0) {
        // 记录省份对应的Index
        reg.index[0] = index0;
        reg.code[0] = provinceList[item0].code;
        // 遍历市列表
        let citys = provinceList[item0].sub;
        for (var item1 = 0; item1 < citys.length; item1++) {
          // 记录当前省下的所有市
          reg.data[1].push({
            code: citys[item1].code,
            name: citys[item1].name,
          });
          // 如果市code对应找到
          if (item1 == index1) {
            // 记录市对应的Index
            reg.index[1] = index1;
            reg.code[1] = citys[item1].code;
            let areas = citys[item1].sub;
            for (var item2 = 0; item2 < areas.length; item2++) {
              reg.data[2].push({
                code: areas[item2].code,
                name: areas[item2].name
              });
              if (item2 == index2) {
                reg.code[2] = areas[item2].code;
                reg.index[2] = index2;
              }
            }
          }
        }
      }
    }
    return reg;
  }
}

module.exports = region;